<?php
class XA_Query {

	protected $db;

	public $table;
	public $alias;
	public $fields = array();
	public $joins = array();
	public $conditions = array();
	public $order_rules = array();
	public $group_rules = array();
	public $offset;
	public $limit;
	public $quote_char = '"';
	public $distinct;
	public $db_type;



	public function __construct($table = null) {
		if (!defined('DB_TYPE') || DB_TYPE === 'mysql') {
			$this->quote_char = '`';
			$this->db_type = 'mysql';
		} else {
			$this->db_type = DB_TYPE;
		}

		if ($table) {
			if (substr_count($table, ' ')) {
				list($this->table, $this->alias) = explode(' ', $table);
			} else {
				$this->table = $this->alias = $table;
			}
		}
	}



	public function get() {
		$qc = $this->quote_char;
		$select_fields = array();
		if (is_array($this->fields)) {
			foreach ($this->fields as $field_name => $expression) {
				if (is_int($field_name)) {
					$select_fields[] = $expression;
				} else {
					$select_fields[] = $expression." AS $qc$field_name$qc";
				}
			}
		}
		if (!$select_fields) {
			$select_fields[] = '*';
		}

		$conditions = '';
		if ($this->conditions) {
			if (is_array($this->conditions)) {
				$conditions = implode('
	AND
	', $this->conditions);
			} else {
				$conditions = $this->conditions;
			}
		}

		$bottom_limit = $top_limit = '';
		if ($this->limit) {
			if ($this->db_type === 'sybase') {
				$top_limit = 'TOP '.intval($this->limit).' ';
			} elseif ($this->db_type === 'postgres') {
				$bottom_limit = 'LIMIT '.intval($this->limit).($this->offset ? ' OFFSET '.intval($this->offset) : '');
			} else {
				$bottom_limit = 'LIMIT '.($this->offset ? intval($this->offset).', ' : ' ').intval($this->limit);
			}
		}

		return '
SELECT '.$top_limit.($this->distinct ? 'DISTINCT' : '').'
	  '.implode('
	, ', $select_fields)."
FROM
	$qc".$this->table."$qc AS $qc".$this->alias."$qc
	".implode('
	', $this->joins).'
'.($conditions ? 'WHERE
	'.$conditions : '').'
'.($this->group_rules ? 'GROUP BY
	'.implode('
	, ', $this->group_rules) : '').'
'.($this->order_rules ? 'ORDER BY
	  '.implode('
	, ', $this->order_rules) : '').'
'.$bottom_limit.'
		';
	}



	/*
	$sql->join('person p', 'p.person_id = other_table.person_id');
	$sql->join('person p', 'person_id'); // join to main table using person_id field
	$sql->join('person', 'person_id'); // use no alias (use table name as alias)
	*/
	public function inner($table, $condition, $index = '') {
		return $this->join('JOIN', $table, $condition, $index);
	}



	public function left($table, $condition, $index = '') {
		return $this->join('LEFT JOIN', $table, $condition, $index);
	}



	public function join($join_type, $table, $condition, $index = '') {
		$qc = $this->quote_char;

		if (!$index) {
			$index = $table;
		}

		if (substr_count($table, ' ')) {
			list($table, $alias) = explode(' ', $table);
		} else {
			$alias = $table;
		}

		if (!substr_count($condition, ' ')) {
			// podano tylko nazwe pola po ktorym mamy złączyc
			$condition = "$qc$alias$qc.$qc$condition$qc = $qc".$this->alias."$qc.$qc$condition$qc";
		}

		$this->joins[$index] = "$join_type $qc$table$qc AS $qc$alias$qc ON $condition";
		return $this;
	}
}
